package com.strongbj.iot.devices.newlight.request.handle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strongbj.core.message.IMessageHandle;
import com.strongbj.core.util.ByteUtil;
import com.strongbj.core.util.ContextUtils;
import com.strongbj.iot.devices.newlight.common.NetMapping;
import com.strongbj.iot.devices.newlight.entity.Reader;
import com.strongbj.iot.devices.newlight.entity.ReaderAwakenData;
import com.strongbj.iot.devices.newlight.entity.T;
import com.strongbj.iot.devices.newlight.message.NewLightMQMessage;
import com.strongbj.iot.devices.newlight.message.NewLightOSMessageFactory;
import com.strongbj.iot.devices.newlight.service.ReaderService;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

public class ReaderAwakenHandle implements IMessageHandle<NewLightMQMessage, Object> {

	private static Logger logger = LogManager.getLogger(ReaderAwakenHandle.class.getName());
	private ReaderService readerService = (ReaderService) ContextUtils.getBean("readerService");
	private NewLightOSMessageFactory newLightOSMessageFactory = (NewLightOSMessageFactory) ContextUtils
			.getBean("newLightOSMessageFactory");

	@Override
	public boolean isHandle(NewLightMQMessage msg) {
		if (msg.getActioncode().equals("readerAwaken")) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Object handle(ChannelHandlerContext ctx, NewLightMQMessage msg) {
		logger.info("====收到Reader静默====");
		ReaderAwakenData data = ((JSONObject) msg.getPostdata()).toJavaObject(ReaderAwakenData.class);
		byte[] sendData = null;
		if (data.getType() == 1) {
			sendData = ByteUtil.hexStringToBytes("AA55");
		} else {
			sendData = ByteUtil.hexStringToBytes("55AA");
		}
		byte[] req = newLightOSMessageFactory.createNewLightOSMessage(ByteUtil.addressToBytes(data.getReaderCode()),
				(byte) 0x3e, sendData, T.getTimeStamp());

		QueryWrapper<Reader> query = new QueryWrapper<>();
		query.eq("code_", data.getReaderCode());
		Reader reader = readerService.getOne(query);
		if (reader != null) {
			ByteBuf bs = Unpooled.copiedBuffer(req);
			Channel channel = NetMapping.getInstance().getChannel(reader.getIp());
			ChannelFuture cf = channel.writeAndFlush(bs);
			cf.addListener(new ChannelFutureListener() {

				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (future.isSuccess()) {
						logger.info("发送打开盘点命令成功 设备编号:{} 命令={}  ", reader.getCode(),
								ByteUtil.byteArrToHexString(req, true));
					} else {
						logger.debug("发送打开盘点命令失败 设备编号:{} 命令={}  ", reader.getCode(),
								ByteUtil.byteArrToHexString(req, true));
					}
				}

			});
		}else {
			// TODO 未做闭环处理
		}
		return null;
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub

	}

}
